package com.fyz.aaronfjava.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author：AaronF
 * @Date：2024/1/25 13:50
 * @description 获取Spring容器Bean工具类 需要spring环境
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    /**
     * spring容器
     */
    private ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

    public <T> T getBean(Class<T> beanClass){
        return applicationContext.getBean(beanClass);
    }

    public <T> Map<String, T> getBeans(Class<T> beanClass){
        return applicationContext.getBeansOfType(beanClass);
    }
}
